package main;

import com.item.manager.Template;
import encounter.Encounter;

public class LootContainer implements Encounter {
    private final String name;
    private final int spotDifficulty;
    private final int openDifficulty;
    private final int copper;

    public LootContainer(Template template) {
        this.name = template.name();
        this.spotDifficulty = template.findAttr("spotDifficulty").asInt();
        this.openDifficulty = template.findAttr("openDifficulty").asInt();
        this.copper = template.findAttr("Money").asInt();
    }

    @Override
    public String toString() {
        return this.name;
    }

    @Override
    public boolean resolve(Hero hero) {
        boolean looted = false;

        int spotRoll = RandomProvider.roll(hero.getSpot());
        int concealRoll = RandomProvider.roll(spotDifficulty);

        if (spotRoll > concealRoll) {
            Logger.log(hero + " finds " + this + ".");

            int openRoll = RandomProvider.roll(hero.getOpenLocks());
            int lockRoll = openDifficulty == 0 ? 0 : RandomProvider.roll(openDifficulty);
            if (openRoll > lockRoll) {
                int loot = RandomProvider.roll(copper);
                Logger.log(hero + " opens " + this + " and gets " + loot + " copper");
                hero.addEvent("Loots " + this + " for " + loot + " copper");
                hero.updateCopper(loot);
                looted = true;
            } else {
                Logger.log(hero + " can't open " + this);
            }
        }

        hero.updateEnergy(-0.01);
        return false;
    }
}
